/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "nform.h"
#include "mdown.h"

#include <QGridLayout>
#include <QTextCursor>
#include <QLabel>
#include <QLineEdit>
#include <QEvent>
#include <QKeyEvent>

NFormEditor::NFormEditor(QWidget* parent) : QTextEdit(parent)
{
}

void NFormEditor::insertFromMimeData(const QMimeData* source)
{
    textCursor().removeSelectedText();
    textCursor().insertText(source->text());
}

/////////////////////////////////////////////////////

NFormEditor* NForm::getEdNcode()
{
    return ed_ncode;
}


NForm::NForm(QWidget* parent) : QWidget(parent)
{
    this->createWidgets();
    this->doSlotsSignals();
    p = NULL;
}

Part* NForm::getEditingPart()
{
    return p;
}

void NForm::clear()
{
	ed_ncode->setText(tr("Start a new document"));
}

void NForm::setEditingPart(Part* _p)
{
    p = _p;
    if(p == NULL)
		ed_ncode->setPlainText(QString());
    else
		ed_ncode->setPlainText(p->getContents());
}

void NForm::createWidgets()
{
    QVBoxLayout* gr = new QVBoxLayout(this);
    QHBoxLayout* layout_title = new QHBoxLayout(this);
    QLabel* titleLabel = new QLabel(tr("Title :"));
    QLabel* licenceLabel = new QLabel(tr("Licence :"));
    QLabel* difficultLabel = new QLabel(tr("Difficult :"));
    QLabel* progressLabel = new QLabel(tr("Progress :"));

    progress = new QSpinBox(this);
    progress->setMaximum(100);
    progress->setMinimum(0);
    progress->setSingleStep(5);


    layout_title->addWidget(progressLabel);
    layout_title->addWidget(progress);

    licence = new QComboBox(this);
    licence->addItem(tr("Unauthorized copy"));
    licence->addItem(tr("CC-by"));
    licence->addItem(tr("CC-by-sa"));
    licence->addItem(tr("CC-by-nc"));
    licence->addItem(tr("CC-by-sa-nc"));
    licence->addItem(tr("CC-by-nd"));
    licence->addItem(tr("CC-by-nc-nd"));

    layout_title->addWidget(licenceLabel);
    layout_title->addWidget(licence);

    difficult = new QComboBox(this);
    difficult->addItem(tr("None"));
    difficult->addItem(tr("1/5"));
    difficult->addItem(tr("2/5"));
    difficult->addItem(tr("3/5"));
    difficult->addItem(tr("4/5"));
    difficult->addItem(tr("5/5"));

    layout_title->addWidget(difficultLabel);
    layout_title->addWidget(difficult);

    title_te = new QLineEdit(this);
    layout_title->addWidget(titleLabel);
    layout_title->addWidget(title_te);

    ed_ncode = new NFormEditor(this);

    ed_preview = new QTextEdit(this);

    ed_preview->setReadOnly(true);
    ed_preview->setAcceptRichText(true);
    
    gr->addLayout(layout_title);

    gr->addWidget(ed_ncode);
    gr->addWidget(ed_preview);

    this->setLayout(gr);
}

void NForm::doSlotsSignals()
{
    connect(ed_ncode, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
}

void NForm::onTextChanged()
{
    doPreview();
    if(p){
    	p->setContents(ed_ncode->toPlainText());
		emit documentChanged();
	}
}

void NForm::doPreview()
{
//    ed_preview->setPlainText(Mdown::toLatex(ed_ncode->toPlainText()));
    ed_preview->setHtml(Mdown::toHtml(ed_ncode->toPlainText()));
}

void NForm::hideTitledOptions()
{
    licence->setEnabled(false);
    progress->setEnabled(false);
    difficult->setEnabled(false);
}

void NForm::displayTitleOptions()
{
    licence->setEnabled(true);
    progress->setEnabled(true);
    difficult->setEnabled(true);
}
